package org.example.elm_xiangmu.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.example.elm_xiangmu.po.Business;
import org.example.elm_xiangmu.po.Orders;
import org.example.elm_xiangmu.result.Result;
import org.example.elm_xiangmu.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/Orders")
@Tag(name ="订单")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Operation(summary = "增加订单")
    @PostMapping ("/createOrders")
    public Result createOrders(@Parameter(description = "用户编号") @RequestParam String userId,
                               @Parameter(description = "商家编号") @RequestParam Integer businessId,
                               @Parameter(description = "送货地址编号") @RequestParam Integer daId,
                               @Parameter(description = "订单总金额") @RequestParam Double orderTotal) {

        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setDaId(daId);
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 获取当前时间并格式化为字符串
        String formattedDate = LocalDateTime.now().format(formatter);
        orders.setOrderDate(formattedDate); // 设置当前时间的字符串表示
        orders.setOrderTotal(orderTotal);
        orders.setOrderState(0);

        Integer orderId = ordersService.createOrders(orders);
        return Result.success(orderId,"增加订单成功");
    }

    @Operation(summary = "根据订单编号查询订单信息，包括所属商家信息，和此订单的所有订单明细信息")
    @GetMapping ("/getOrdersById")
    public Result getOrdersById(@Parameter(description = "订单编号") @RequestParam Integer orderId) {

        return ordersService.getOrdersById(orderId);

    }


    @Operation(summary = "根据用户编号查询此用户的所有订单信息")
    @GetMapping ("/listOrdersByUserId")
    public Result listOrdersByUserId(@Parameter(description = "用户编号") @RequestParam String userId) {

        return ordersService.listOrdersByUserId(userId);

    }

}
